package fr.isima.tools;

import java.io.File;
import java.io.FilePermission;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import android.util.Log;

public class FileUtils
{
    private static final String TAG = "FileUtils";

    public static boolean copyFile(String fileToCopy, String outputDir, String copyFile)
    {
        boolean succes = false;
        File output = new File(outputDir);
        File src = new File(fileToCopy);
        File dest = new File(output + copyFile);

        try
        {
            if(!output.exists())
            {
                FilePermission perm = new FilePermission(outputDir, "write");
                output.mkdir();
                
                perm = null;
            }
            if(!dest.exists())
            {
                dest.createNewFile();
            }
            FileReader in = new FileReader(src);
            FileWriter out = new FileWriter(dest);
            int c;

            while ((c = in.read()) != -1)
                out.write(c);

            in.close();
            out.close();

            succes = true;
        }
        catch (IOException e)
        {
            Log.e(TAG, "FileUtils:copyFile" + e.getMessage());
        }
        return succes;
    }

    public static boolean deleteFile(String filePath)
    {
        boolean success = false;
        File file = new File(filePath);
        if (file.exists() && file.canWrite() && file.isFile())
        {
            file.delete();
            success = true;
        }

        return success;
    }
}
